﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Threading;
using System.Xaml;

namespace TauschFormat2
{
	/// <summary>
	/// Interaktionslogik für PokemonEdit.xaml
	/// </summary>
	public partial class PokemonEdit : Window
	{
        PokeDataDataSet pkData = new PokeDataDataSet();
        PokeDataDataSetTableAdapters.PokemonTableAdapter pkTableAdapter = new PokeDataDataSetTableAdapters.PokemonTableAdapter();

		public PokemonEdit()
		{
			this.InitializeComponent();
			
			// Fügen Sie Code, der bei der Objekterstellung erforderlich ist, unter diesem Punkt ein.
		}

        private void bttnOK_Click(object sender, RoutedEventArgs e) {

            Thread UpdateThread = new Thread(new ParameterizedThreadStart(Update));

            string[] insert = new string[txtPokemon.LineCount];
            for (int i = 0; i < txtPokemon.LineCount; i++)
            {
                insert[i] = txtPokemon.GetLineText(i).Replace("\r\n", "");
            }

            UpdateThread.Start(insert);


            PlsWait wait = new PlsWait();
            wait.ShowDialog();
            while (UpdateThread.ThreadState != ThreadState.Stopped) ;
            wait.Close();
            pkTableAdapter.Update(pkData.Pokemon);
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            pkTableAdapter.Fill(pkData.Pokemon);
            foreach (DataRow row in pkData.Pokemon)
            {
                txtPokemon.Text += row["Pokemonname"].ToString() + "\r\n";
            }
        }
        public void Update(object tb)
        {
            string[] tbs = (string[])tb;
            pkTableAdapter.Update(pkData.Pokemon);
            /*var x = from r in pkData.Pokemon select r.id;
            foreach (int r in (EnumerableRowCollection<int>)x)
            {
                pkTableAdapter.Delete(r);
            }*/
            pkData.Pokemon.Rows.Clear();
            pkTableAdapter.Update(pkData.Pokemon);
            if (tbs.Length > 0)
            {
                pkTableAdapter.Update(pkData.Pokemon);
                for (int i = 0; i < txtPokemon.LineCount; i++)
                {
                    pkData.Pokemon.AddPokemonRow(tbs[i]);
                }
            }
        }
	}
}